﻿using Lottery.Base;
using Lottery.Db;
using Lottery.Real;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lottery.Console
{
    class Program
    {
        static void Main(string[] args)
        {
            var lr = new NationalLotteryUkraineCsvRepository(@"C:\Users\Max\Downloads\super_loto_csv_1-1326__unsort\SuperLoto_Results__918-1326.csv");
            var r = lr.GetGameHistory();

            //var numbers = new MyOwnNumbersSelector().SelectNumbers(6, r);
            //var combination = new MyOwnCombinationsSelector().SelectCombinations(numbers);


            //RunHistoricalReview(r, new SimpleNumbersSelector( new List<int> { 5, 8, 12, 15, 25, 37, 42, 48, 6, 19, 52, 30 }), new CsvSystemCombinationsSelector(@"C:\Users\Max\Downloads\super_loto_csv_1-1326__unsort\System12num42com.csv"));
            RunHistoricalReview(r, new SimpleNumbersSelector(new List<int> { 1, 3, 5, 8, 7, 12, 17, 15, 22, 29, 31, 20, 44, 50, 41, 45, 33, 39, 25, 37, 42, 48, 6, 19, 52, 30 }), new CsvSystemCombinationsSelector(@"C:\Users\Max\Downloads\super_loto_csv_1-1326__unsort\System26num130com.csv"));
            //RunHistoricalReview(r, new MyOwnNumbersSelector(), new MyOwnCombinationsSelector());            
        }

        private static void RunHistoricalReview(IGameHistory gameHistory, INumbersSelector numbersSelector, ICombinationsSelector combinationsSelector)
        {
            var firstRound = 11;
            var overallZatrati = 0;
            var overallViigrish = 0;
            
            for(int i=firstRound; i<gameHistory.History.Count(); i++)            
            {
                var historyForRound = new GameHistory() { History = gameHistory.History.Take(i - 1) };
                var roundCombination = gameHistory.History.ElementAt(i).NumberSet;

                var myNumbers = numbersSelector.SelectNumbers(combinationsSelector.RequiredNumbersCount, historyForRound);
                var myCombinations = combinationsSelector.SelectCombinations(myNumbers);

                var fives = 0;
                var fourth = 0;
                var sixes = 0;
                var threes = 0;
                foreach (var myCombination in myCombinations)
                {
                    int sovp = 0;
                    foreach (var number in roundCombination.Numbers)
                    {
                        foreach (var myNumber in myCombination.Numbers)
                        {
                            if (myNumber == number)
                                sovp++;
                        }
                    }

                    if (sovp == 3)
                        threes++;

                    if (sovp == 4)
                        fourth++;

                    if (sovp == 5)
                        fives++;

                    if (sovp == 6)
                        sixes++;
                }

                var zatrati = myCombinations.Count() * 2;

                if (threes > 0 || fourth > 0 || fives > 0 || sixes > 0)
                {
                    var viigrish = threes * 2 + fourth * 15 + fives * 4000 + sixes * 1000000;

                    overallZatrati += zatrati;
                    overallViigrish += viigrish;
                    System.Console.WriteLine(String.Format("Running round {0}, win: 3 -- {1}, 4 -- {2}, 5 -- {3}, 6 -- {4}, amount -- {5}, viigrish -- {6}, zatrati ------- {7}", i, threes, fourth, fives, sixes, zatrati, viigrish, viigrish - zatrati));
                }

                if (fives > 0)
                {
                    System.Console.WriteLine(String.Format(" ->>>>>>>>>>>>>>>>>>>>>>> Running round {0}, Not bad", i));
                }


                if (sixes > 0)
                {
                    System.Console.WriteLine(String.Format(" ->>>>>>>>>>>>>>>>>>>>>>> Running round {0}, WINNER", i));
                }
            }

            System.Console.WriteLine(String.Format("Overall: zatrati -- {0}, viigrish -- {1}, spend -- {2}", overallZatrati, overallViigrish,  overallViigrish - overallZatrati));
        }
    }
}
